//
//  HZAreaPickerView.m
//  areapicker
//
//  Created by Cloud Dai on 12-9-9.
//  Copyright (c) 2012年 clouddai.com. All rights reserved.
//

#import "HZAreaPickerView.h"
#import <QuartzCore/QuartzCore.h>

#define kDuration 0.3

@interface HZAreaPickerView ()
{
    NSArray *provinces, *cities, *areas;
}


@end

@implementation HZAreaPickerView

@synthesize delegate=_delegate;
@synthesize pickerStyle=_pickerStyle;
@synthesize locate=_locate;

//- (void)dealloc
//{
//    [_locate release];
//    [_locatePicker release];
//    [provinces release];
//    [super dealloc];
//}




-(HZLocation *)locate
{
    if (_locate == nil) {
        _locate = [[HZLocation alloc] init];
    }
    
    return _locate;
}

- (id)initWithStyle:(HZAreaPickerStyle)pickerStyle delegate:(id<HZAreaPickerDelegate>)delegate
{
    
    self = [[[NSBundle mainBundle] loadNibNamed:@"HZAreaPickerView" owner:self options:nil] objectAtIndex:0] ;
    
    if (self) {
        self.delegate = delegate;
        self.pickerStyle = pickerStyle;
        self.locatePicker.dataSource = self;
        self.locatePicker.delegate = self;
        
        //加载数据
        if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
            provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]];
            cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
            
            self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"state"];
            self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];
            
            areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
            if (areas.count > 0) {
                self.locate.district = [areas objectAtIndex:0];
            } else{
                self.locate.district = @"";
            }
            
        } else{
            provinces = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"city.plist" ofType:nil]];
            cities = [[provinces objectAtIndex:0] objectForKey:@"cities"];
            self.locate.state = [[provinces objectAtIndex:0] objectForKey:@"state"];
            self.locate.city = [cities objectAtIndex:0];
        }
    }
        
    return self;
    
}



#pragma mark - PickerView lifecycle

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        return 3;
    } else{
        return 2;
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return [provinces count ];
            break;
        case 1:
            return [cities count];
            break;
        case 2:
            if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
                return [areas count];
                break;
            }
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    
    
    if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        switch (component) {
            case 0:
                return [[provinces objectAtIndex:row] objectForKey:@"state"];
                break;
            case 1:
                return [[cities objectAtIndex:row] objectForKey:@"city"];
                break;
            case 2:
                if ([areas count] > 0) {
                    return [areas objectAtIndex:row];
                    break;
                }
            default:
                return  @"";
                break;
        }
    } else{
        switch (component) {
            case 0:
                return [[provinces objectAtIndex:row] objectForKey:@"state"];
                break;
            case 1:
                return [cities objectAtIndex:row];
                break;
            default:
                return @"";
                break;
        }
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    
    NSLog(@"fffffffffffff  %zd",component);

    
    if (self.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        switch (component) {
            case 0:
                cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];
                [self.locatePicker selectRow:0 inComponent:1 animated:YES];
                [self.locatePicker reloadComponent:1];
                
                areas = [[cities objectAtIndex:0] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                self.locate.state = [[provinces objectAtIndex:row] objectForKey:@"state"];
                self.locate.city = [[cities objectAtIndex:0] objectForKey:@"city"];
                if ([areas count] > 0) {
                    self.locate.district = [areas objectAtIndex:0];
                } else{
                    self.locate.district = @"";
                }
                break;
            case 1:
                areas = [[cities objectAtIndex:row] objectForKey:@"areas"];
                [self.locatePicker selectRow:0 inComponent:2 animated:YES];
                [self.locatePicker reloadComponent:2];
                
                self.locate.city = [[cities objectAtIndex:row] objectForKey:@"city"];
                if ([areas count] > 0) {
                    self.locate.district = [areas objectAtIndex:0];
                } else{
                    self.locate.district = @"";
                }
                break;
            case 2:
                if ([areas count] > 0) {
                    self.locate.district = [areas objectAtIndex:row];
                } else{
                    self.locate.district = @"";
                }
                break;
            default:
                break;
        }
    } else{
        switch (component) {
            case 0:
                cities = [[provinces objectAtIndex:row] objectForKey:@"cities"];
                [self.locatePicker selectRow:0 inComponent:1 animated:YES];
                [self.locatePicker reloadComponent:1];
                
                self.locate.state = [[provinces objectAtIndex:row] objectForKey:@"state"];
                self.locate.city = [cities objectAtIndex:0];
                break;
            case 1:
                self.locate.city = [cities objectAtIndex:row];
                break;
            default:
                break;
        }
    }
    
    if([self.delegate respondsToSelector:@selector(pickerDidChaneStatus:)]) {
        [self.delegate pickerDidChaneStatus:self];
    }

}


#pragma mark - animation

- (void)showInView:(UIView *) view
{
    
//    self.view1.frame =CGRectMake(0, self.frame.size.height+83 , self.frame.size.width, self.frame.size.height +100 );
//    self.view1.backgroundColor = [UIColor redColor];
//    
//    
//    
//   UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, self.frame.size.height+83, self.frame.size.width, self.frame.size.height +100)];
//  view1.backgroundColor = [UIColor redColor];
//   [view addSubview:view1];
////
////    
////    
////     self.frame = CGRectMake(0, view.frame.size.height, self.frame.size.width, self.frame.size.height+60);
////     [view1 addSubview:self];
////    
////    
////    
////    
//    self.frame = CGRectMake(0, self.view1.frame.size.height+60, self.frame.size.width, self.frame.size.height +290) ;
//    [view1 addSubview:self];
//    
//    
//    
//
//   
//    
//    
    
    if ([UIScreen mainScreen].bounds.size.height == 480)
    {
        self.frame = CGRectMake(0, view.frame.size.height-50, self.frame.size.width, self.frame.size.height-40);
        
    }
    else{
        
        self.frame = CGRectMake(0, view.frame.size.height, self.frame.size.width, self.frame.size.height+36);
    }

    [view addSubview:self];
    
//自己后期添加又取消掉的确定与取消的方法
    
    
//    self.btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
//    
//    [self.btn1 setTitle:@"确定" forState:UIControlStateNormal];
// 
//    self.btn1.backgroundColor = GREEN_COLOR;
//        self.btn1.frame = CGRectMake(view.frame.size.width-60, view.frame.size.height*0.5+2, 60, 25);
//        [self.btn1 addTarget:self action:@selector(cancelLocatePicker) forControlEvents:UIControlEventTouchUpInside];
//    
//        [view addSubview:self.btn1];
//    
//    
//    
//    self.btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.btn2 setTitle:@"取消" forState:UIControlStateNormal];
//    self.btn2.backgroundColor = GREEN_COLOR;
//    self.btn2.frame = CGRectMake(0, view.frame.size.height*0.5+2, 60, 25);
//     [self.btn2 addTarget:self action:@selector(cancelLocatePicker) forControlEvents:UIControlEventTouchUpInside];
//    
//    [view addSubview:self.btn2];
    
    
    [UIView animateWithDuration:0.1 animations:^{
       self.frame = CGRectMake(0, view.frame.size.height - self.frame.size.height, self.frame.size.width, self.frame.size.height);
        
//        view1.frame =  CGRectMake(0, view.frame.size.height - 270 , self.frame.size.width, self.frame.size.height-236);
//  
   }];
    
    
}

//点击button取消pickView
//-(void)cancelLocatePicker
//{
//    [self cancelPicker];
//    self.locatePicker.delegate = nil;
//    self.locatePicker = nil;
//    
//    //移除button键
//    
//    [self.btn1 removeFromSuperview];
//    [self.btn2 removeFromSuperview];
//}






//返回指定列，行的高度，就是自定义行的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return  25;
    
}



- (void)cancelPicker

{
    
    
    [UIView animateWithDuration:0.3
                     animations:^{
                         self.frame = CGRectMake(0, self.frame.origin.y+self.frame.size.height, self.frame.size.width, self.frame.size.height);
                         
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                                                
                     }];
    
}

@end
